#pragma once

#include "ftp/Types.h"
#include "ftp/IDtpManager.h"

#include <queue>
#include <memory>
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/noncopyable.hpp>

namespace ftp
{

class AsioDtpManager : public IDtpManager, private boost::noncopyable
{
public:
	AsioDtpManager(ba::io_service* ioService, int port);
	virtual ~AsioDtpManager() {}

	virtual int getPort();
	virtual std::string getIp();

	virtual void acceptOutputDtp(std::auto_ptr<IDtpSource> dtpSource, IDtpObserver* dtpObserver);
	virtual void acceptInputDtp(std::auto_ptr<IDtpSource> dtpSync, IDtpObserver* dtpObserver);

private:
	struct DataConnection
	{
		SocketSPtr socket;
		std::auto_ptr<IDtpSource> dataProducer;
		IDtpObserver* observer;
	};

private:
	int m_port;
	ba::io_service* m_ioService;
	tcp::acceptor m_acceptor;

	std::queue<SocketSPtr> m_acceptedSockets;
	boost::ptr_vector<IDtpSource> m_queuedDataProviders;
	std::queue<IDtpObserver*> m_queuedObservers;

	boost::ptr_vector<DataConnection> m_dataConnections;
};

}

